CONTENTS | INDEX | PREV | NEXT
 strncmp

   NAME
    strncmp  - compare two strings up to a maximum number of characters

   SYNOPSIS
    int r = strncmp(s1, s2, n);
    const char *s1;
    const char *s2;
    int n;

   FUNCTION
    Compares two strings, returning:
        -1  s1 < s2
         0  s1 == s2
         1  s1 > s2

    strncmp() works like strcmp() but only up to n characters will be
    compared.  If all characters compare when n is reached 0 is
    returned indicating that the strings matched.  Fewer characters
    might be compared if either string terminates (w/ a nul) before
    the maximum is reached or a compare fails (scan is stopped and -1
    or 1 is returned immediately)

   NOTE
    strncmp() converts the chars in the string to unsigned quantities
    when comparing them.  However, for portability you should not
    strncmp() strings containing negative characters (bit 7 set) for
    anything other than checking the result against 0.  Use the
    memcmp() routine instead.

   EXAMPLE
    #include <stdio.h>
    #include <string.h>
    #include <assert.h>

    main()
    {
        char *s1 = "abcaq";
        char *s2 = "abcdr";
        char *s3 = "abcxs";
        char *s4 = "abcdxx";
        char *s5 = "abc";
        char *x2 = "abcdt";
        int r;

        r = strncmp(s2, x2, 4);
        assert(r == 0);

        r = strncmp(s2, s1, 4);
        assert(r > 0);

        r = strncmp(s2, s3, 4);
        assert(r < 0);

        r = strncmp(s2, s4, 8);
        assert(r < 0);

        r = strncmp(s2, s5, 4);
        assert(r > 0);

        return(0);
    }

   INPUTS
    char *s1;   pointer to first string
    char *s2;   pointer to second string
    int n;      maximum number of characters to compare

   RESULTS
    int r;      result: -1, 0, or 1.

   SEE ALSO
    strncmp, stricmp